package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.adapter.ContributorsAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.WidgetContributorsViewBinding
import com.polaris.live.net.userService
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router

/**
 * 贡献者
 * @author Created by 天晴 on 2024/5/29 9:38
 * @since 1.0.0
 **/
class ContributorsView(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    private val mBinding = WidgetContributorsViewBinding.inflate(LayoutInflater.from(context), this)

    private val mAdapter by lazy {
        ContributorsAdapter()
    }
    private var mViewModel: BaseViewModel? = null
    private var anchorId: Long = 0

    init {
        mAdapter.setOnItemClickListener { _, _, position ->
            val data = mAdapter.getItem(position)
            Router.instance.setPath(RoutePath.MESSAGE_ACTIVITY)
                .withInt("page", UserConst.From.MY)
                .apply {
                    data.user.also {
                        withString("nickname", it.nickname)
                        withString("avatar", it.avatar)
                        it.avatarFrame?.also { af -> withString("avatarFrame", af) }
                        it.chatBubble?.also { cb -> withString("chatBubble", cb) }
                    }
                }
                .withLong("userId", data.user.userId)
                .navigation(context)
        }
        mBinding.apply {
            contributorsRecycler.apply {
                adapter = mAdapter
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            }
            contributorsTitle.setOnSingleClickListener {
                Router.builder(RoutePath.CONTRIBUTION_LIST_ACTIVITY)
                    .withLong("anchorId", anchorId)
                    .navigation(context)
            }
            pdShowNodata()
        }

    }


    fun initView(anchorId: Long, mViewModel: BaseViewModel) {
        this.anchorId = anchorId
        this.mViewModel = mViewModel
    }

    fun update() {
        getRankUser()
    }

    private fun getRankUser() {
        mViewModel?.request({ userService.getRankUser(anchorId) }, {
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
            pdShowNodata()
        }, {
            pdShowNodata()
        })
    }

    private fun pdShowNodata() {
        if (mAdapter.data.isEmpty()) {
            visibility = View.GONE
            mBinding.nodatatipsTv.visibility = View.VISIBLE
        } else {
            visibility = View.VISIBLE
            mBinding.nodatatipsTv.visibility = View.GONE
        }
    }
}